#include "playlistentry.h"
#include "audiofile.h"
#include "playlist.h"

#include "debugmodule.h"

const QString PlaylistEntry::DATE_TIME_FORMAT = "dd.MM.yyyy hh:mm:ss";

PlaylistEntry::PlaylistEntry(Playlist *i_pPlaylist, AudioFile *i_pAudioFile,
                             const QString& i_filepath,
                             const QString &i_title, const QString &i_artist,
                             const QString &i_album, const QString &i_genre,
                             const QString &i_track, const QString &i_year,
                             const QString &i_date1, const QString &i_date2,
                             const QString &i_empty1, const QString &i_empty2) :
    m_pAudioFile(nullptr),m_pPlaylist(i_pPlaylist),
    m_completeAndValid(false),
    m_title(i_title), m_artist(i_artist),
    m_album(i_album), m_genre(i_genre),
    m_date1(i_date1), m_date2(i_date2),
    m_empty1(0), m_empty2(0)
{
    setTrack(i_track, false);
    setYear(i_year, false);
    m_empty1 = i_empty1.toUInt();
    m_empty2 = i_empty2.toUInt();

    setFilePath(i_filepath);
    setAudioFile(i_pAudioFile);
    checkCompleteAndValid();
}

PlaylistEntry::~PlaylistEntry()
{
    if (m_pAudioFile)
        m_pAudioFile->removePlaylistEntry(this);
}

void PlaylistEntry::checkCompleteAndValid()
{
    const bool lastCompleteAndValid = m_completeAndValid;
    m_completeAndValid = false;

    // Alle Felder befüllt?
    if (!m_title.isEmpty() && !m_artist.isEmpty() &&
            !m_album.isEmpty() && !m_genre.isEmpty() &&
            m_track > 0 && m_year > 1900 && m_year < 2100)
    {
        m_completeAndValid = true;
    }

    if (lastCompleteAndValid != m_completeAndValid)
        emit completeStateChanged(m_completeAndValid);
}

void PlaylistEntry::setFilePath(const QString &i_path)
{
    m_filepath = i_path;
    m_filename = QFileInfo(i_path).completeBaseName();
    emit dataChanged();
}

void PlaylistEntry::setAudioFile(AudioFile *i_pAudioFile)
{
    if (m_pAudioFile)
    {
        m_pAudioFile->removePlaylistEntry(this);
        disconnect(m_pAudioFile, 0, this, 0);
    }

    m_pAudioFile = i_pAudioFile;

    if (m_pAudioFile)
    {
        m_pAudioFile->addPlaylistEntry(this);
        connect(m_pAudioFile, SIGNAL(dataChanged()), SIGNAL(dataChanged()));
    }
}

void PlaylistEntry::removeAudioFile()
{
    if (m_pAudioFile)
        disconnect(m_pAudioFile, 0, this, 0);

    m_pAudioFile = nullptr;
}

void PlaylistEntry::refreshTags()
{
    if (m_pAudioFile)
    {
        m_pAudioFile->readTags();

        // Pipes durch Unterstrich ersetzen,
        // da Pipe Trennzeichen für MPL-Playliste ist
        m_artist = m_pAudioFile->artist();
        m_artist.replace('|','_');
        m_title = m_pAudioFile->title();
        m_title.replace('|','_');
        m_album = m_pAudioFile->album();
        m_album.replace('|','_');
        m_genre = m_pAudioFile->genre();
        m_genre.replace('|','_');
        setTrack(m_pAudioFile->track(), false);
        setYear(m_pAudioFile->year(), false);

        checkCompleteAndValid();
        m_pPlaylist->setChanged();

        //emit dataChanged();
    }
}

void PlaylistEntry::setTitle(const QString &i_value)
{
    if (i_value != m_title)
    {
        m_title = i_value;
        m_title.replace('|','_');
        checkCompleteAndValid();
        m_pPlaylist->setChanged();

        emit dataChanged();
    }
}

void PlaylistEntry::setArtist(const QString &i_value)
{
    if (i_value != m_artist)
    {
        m_artist = i_value;
        m_artist.replace('|','_');
        checkCompleteAndValid();
        m_pPlaylist->setChanged();

        emit dataChanged();
    }
}

void PlaylistEntry::setAlbum(const QString &i_value)
{
    if (i_value != m_album)
    {
        m_album = i_value;
        m_album.replace('|','_');
        checkCompleteAndValid();
        m_pPlaylist->setChanged();

        emit dataChanged();
    }
}

void PlaylistEntry::setGenre(const QString &i_value)
{
    if (i_value != m_genre)
    {
        m_genre = i_value;
        m_genre.replace('|','_');
        checkCompleteAndValid();
        m_pPlaylist->setChanged();

        emit dataChanged();
    }
}

void PlaylistEntry::setTrack(const QString &i_value, bool i_emit)
{
    QString value;

    // Zahl vor Schrägstrich verwenden (z.B. 5/16)
    const int idx = i_value.indexOf('/');
    if (idx > -1)
        value = i_value.left(idx);
    else
        value = i_value;

    const uint track = value.toUInt();

    if (track != m_track)
    {
        m_track = track;

        if (i_emit)
        {
            checkCompleteAndValid();
            m_pPlaylist->setChanged();

            emit dataChanged();
        }
    }
}

void PlaylistEntry::setYear(const QString &i_value, bool i_emit)
{
    const uint year = i_value.toUInt();

    if (year != m_year)
    {
        m_year = year;

        if (i_emit)
        {
            checkCompleteAndValid();
            m_pPlaylist->setChanged();

            emit dataChanged();
        }
    }
}
